package hd.util.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import hd.util.R;
import hd.util.ScreenHelper;

/**
 * 带有阴影的View
 * <p/>
 * Created by haidy on 14/12/17.
 */
public class ShadowView extends View {
    private int backgroundColor = Color.WHITE;
    private int offsetX = 0;
    private int offsetY = 0;
    private int shadowColor = Color.argb(64, 0, 0, 0);
    private Paint backgroundPaint;
    private int shadowRadius = ScreenHelper.dp2px(getContext(), 5);
    private int cornerRadius = ScreenHelper.dp2px(getContext(), 2);

    public ShadowView(Context context) {
        this(context, null);
    }

    public ShadowView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShadowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs, defStyleAttr);
    }

    private void init(AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ShadowView, defStyleAttr, 0);
        if (typedArray != null) {
            int count = typedArray.getIndexCount();
            int index;
            for (int i = 0; i < count; i++) {
                index = typedArray.getIndex(i);
                if (index == R.styleable.ShadowView_backgroundColor) {
                    backgroundColor = typedArray.getColor(index, Color.WHITE);
                } else if (index == R.styleable.ShadowView_offsetX) {
                    offsetX = typedArray.getDimensionPixelSize(index, 0);
                } else if (index == R.styleable.ShadowView_offsetY) {
                    offsetY = typedArray.getDimensionPixelSize(index, 0);
                } else if (index == R.styleable.ShadowView_shadowColor) {
                    shadowColor = typedArray.getColor(index, Color.GRAY);
                } else if (index == R.styleable.ShadowView_shadowRadius) {
                    shadowRadius = typedArray.getDimensionPixelSize(index, ScreenHelper.dp2px(getContext(), 5));
                } else if (index == R.styleable.ShadowView_cornerR) {
                    cornerRadius = typedArray.getDimensionPixelSize(index, ScreenHelper.dp2px(getContext(), 2));
                }
            }
            typedArray.recycle();
        }
        backgroundPaint = new Paint();
        backgroundPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        backgroundPaint.setAntiAlias(true);
        backgroundPaint.setColor(backgroundColor);
        backgroundPaint.setShadowLayer(shadowRadius, offsetX, offsetY, shadowColor);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRoundRect(new RectF(shadowRadius,
                        shadowRadius,
                        getWidth() - (shadowRadius),
                        getHeight() - (shadowRadius)),
                cornerRadius,
                cornerRadius,
                backgroundPaint);
    }
}
